package com.qiangesoft.flowable.controller;

import com.qiangesoft.flowable.constant.ProcessConstant;
import com.qiangesoft.flowable.constant.R;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 流程定义 控制器
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@RestController
@RequestMapping("/definition")
public class ProcessDefinitionController {

    @Autowired
    private RepositoryService repositoryService;

    @GetMapping("/deploy")
    public R deploy() {
        // 部署流程
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/test.bpmn20.xml")
                .deploy();
        return R.ok(deployment);
    }

    @GetMapping("/list")
    public R list() {
        // 查询部署流程
        List<Deployment> deploymentList = repositoryService.createDeploymentQuery()
                .processDefinitionKey(ProcessConstant.PROCESS_KEY)
//                .deploymentName("")
                .orderByDeploymentTime().desc()
                .list();
        return R.ok(deploymentList);
    }

    @GetMapping("/get")
    public R get() {
        Deployment deployment = repositoryService.createDeploymentQuery()
                .processDefinitionKey(ProcessConstant.PROCESS_KEY)
//                .deploymentName("")
                .latest()
                .singleResult();
        return R.ok(deployment);
    }

}
